/**
 * 
 */
package com.expense.taglib;

import java.io.IOException;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import javax.xml.rpc.ServiceException;

import com.expense.dao.Dao;
import com.expense.services.BasicServices;


/**
 * @author :longcong
 * @date :Apr 9, 2010
 */
public class getValueByTableAndColumnTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String idValue;//id 值
	private String idName; //id 列名
	private String colname; //要查询的列名
	private String tabname; //表名

	
	
	 

	/**
	 * @return the idValue
	 */
	public String getIdValue() {
		return idValue;
	}

	/**
	 * @param idValue the idValue to set
	 */
	public void setIdValue(String idValue) {
		this.idValue = idValue;
	}

	/**
	 * @return the idName
	 */
	public String getIdName() {
		return idName;
	}

	/**
	 * @param idName the idName to set
	 */
	public void setIdName(String idName) {
		this.idName = idName;
	}

	/**
	 * @return the colname
	 */
	public String getColname() {
		return colname;
	}

	/**
	 * @param colname the colname to set
	 */
	public void setColname(String colname) {
		this.colname = colname;
	}

	/**
	 * @return the tabname
	 */
	public String getTabname() {
		return tabname;
	}

	/**
	 * @param tabname the tabname to set
	 */
	public void setTabname(String tabname) {
		this.tabname = tabname;
	}

	 

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return super.doStartTag();
	}

	@SuppressWarnings("unchecked")
	public String getValue() {
			String ValueStr = "";
			String sql = "SELECT " + this.colname + " FROM " + this.tabname+ " WHERE "+this.idName+"='" + this.idValue + "'";
			 
			Dao dao=new Dao();
			ResultSet rs =dao.selectData(sql);
			if(rs!=null){
				try {
					if(rs.next()){
						ValueStr=rs.getString(1);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			dao.CloseCon();
		return ValueStr;
	}

	@SuppressWarnings("static-access")
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		
		JspWriter out = pageContext.getOut();
		String ValueStr = this.getValue();

		try {
			out.print(ValueStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.SKIP_BODY;
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		super.release();
	}
}
